#include "./tolf.h"
#include <iostream>
#include <fstream>
namespace whh
{
    using std::cerr;
    using std::cout;
    using std::endl;
    Tolf::Tolf(fs::path d) : dir(d)
    {
    }
    void Tolf::operator()()
    {
        if (!fs::exists(dir))
        {
            cerr << "路径不存在:" << dir.string() << endl;
            return;
        }
        cout << "开始处理:" << dir.string() << endl;
        // 遍历目录
        for (auto &entry : fs::recursive_directory_iterator(dir))
        {
            if (!fs::is_regular_file(entry))
            {
                continue;
            }
            cout << "开始处理文件:" << entry.path().string() << endl;
            std::ifstream inputStream(entry.path(), std::ios::binary);
            if (!inputStream) // 此处使用了 operator!
            {
                cerr << "文件打开失败：" << entry.path().string() << endl;
                return;
            }
            std::ostringstream outStr;
            char buffer[1]; // 设置缓冲区大小为1
            while (inputStream.read(buffer, sizeof(buffer)))
            {
                if (buffer[0] != '\r')
                {
                    outStr << buffer[0];
                }
            }
            // 关闭输入流
            inputStream.close();
            // cout << "转换内容:" << outStr.str() << endl;
            // 打开输出流
            std::ofstream outStream(entry.path(), std::ios::binary);
            outStream << outStr.str();
            // 关闭输出流
            outStream.close();
        }
    }
}